package stack;

public class ArrayStack {

    private Object[] m_arrObj;
    private int m_iSize;

    public ArrayStack() {
        this(10);
    }

    public ArrayStack(int iCapacity) {
        this.m_iSize = 0;
        this.m_arrObj = new Object[iCapacity];
    }

    public void push(Object obj) {
        if (this.m_iSize == this.m_arrObj.length) {
            this.reSize(this.m_arrObj.length * 2);
        }

        this.m_arrObj[this.m_iSize] = obj;
        this.m_iSize++;
    }

    public Object pop() {
        if (this.m_iSize == 0) {
            return null;
        }

        if (this.m_iSize == this.m_arrObj.length / 4
                && this.m_iSize / 2 != 0) {
            reSize(this.m_arrObj.length / 2);
        }

        return m_arrObj[--this.m_iSize];
    }

    private void reSize(int iCapacity) {
        if (iCapacity <= 0) {
            return;
        }

        Object[] arrObj = new Object[iCapacity];
        for (int i = 0; i < this.m_iSize; i++) {
            arrObj[i] = this.m_arrObj[i];
        }
        this.m_arrObj = arrObj;
    }

    public void getSizeAndCount() {
        System.out.println("Size " + this.m_arrObj.length + ", Count " + this.m_iSize);
    }
}
